package dao

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"perfect-admin/global"
)

var (
	DB *gorm.DB
	DBErr error
)


func Close() {
	err := DB.Close()
	if err != nil {
		panic(err)
	}
}

//初始化数据库并产生数据库全局变量
func InitMysql() {
	admin := global.CONFIG.Mysql
	if DB, DBErr = gorm.Open("mysql", admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); DBErr != nil {
		global.LOG.Error("DefaultDB 数据库启动异常", DBErr)
	} else {
		DB.DB().SetMaxIdleConns(admin.MaxIdleConns)
		DB.DB().SetMaxOpenConns(admin.MaxOpenConns)
		DB.LogMode(admin.LogMode)
	}
}